package moe.cat.com.moelight.Utils;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.content.ContextCompat;

import com.tbruyelle.rxpermissions2.Permission;
import com.tbruyelle.rxpermissions2.RxPermissions;

import moe.cat.com.moelight.MainActivity;

/***
 * 申请用户权限辅助方法。
 * 需要引用https://github.com/tbruyelle/RxPermissions
 * 具体参考GIT
 *
 */

public class ApplicationPermissions {

    /**
     * 申请相机权限
     *
     * @param object
     */
    public static void Camera(Object object) {


        Permissions(object, Manifest.permission.CAMERA);

    }

    /**
     * 判断程序是否存在相机权限
     *
     * @return
     */
    public static boolean CameraExits(Context context) {
        return ExitPermission(context,Manifest.permission.CAMERA);

    }

    /**
     * 判断权限是否存在  存在返回true
     *
     * @param permission
     * @return
     */
    public static boolean ExitPermission(Context context,String permission) {
        int permissionCheck = ContextCompat.checkSelfPermission(context,
                permission);
        LogHelper.PrintLog("权限动态码：" + permissionCheck);
        if (permissionCheck ==0)
            return true;
        else return false;
    }

    /**
     * 申请权限通用方法
     */
    public static void Permissions(Object object, String permission) {
        RxPermissions rxPermissions = new RxPermissions((FragmentActivity) object); // where this is an Activity or Fragment instance
//
        rxPermissions
                .request(permission)
                .subscribe(granted -> {
                    if (granted) { // Always true pre-M
                        // I can control the camera now
                        LogHelper.PrintLog("权限存在");

                    } else {
                        // Oups permission denied
                        LogHelper.PrintLog("权限不存在");
                    }
                });
    }

}
